<template>
    <div class="time-picker">
        <!-- 时间选择器 -->
        <div class="year">
            <div class="picker" @click="changeoption">
                <div v-if="typevalue == 'year'">{{ props.selected  }}年</div>
                <div v-if="typevalue == 'month'">{{ props.selected  }}月</div>
                <div v-if="typevalue == 'date'">{{ props.selected  }}日</div>
                <img src="../src/assets/static/image/right.png" alt="" class="select"
                    :style="{ transform: 'rotate(' + rot + 'deg)' }">
            </div>
            <div class="year-choose-con" :style="{ height: optionConH + 'px' }">
                <div class="year-choose">
                    <div class="year-items" v-for="(item,index) in props.list" :key="item.id" @click="handelSelect(item.text)">
                        <div v-if="typevalue == 'year'">{{ item.text  }}年</div>
                <div v-if="typevalue == 'month'">{{ item.text  }}月</div>
                <div v-if="typevalue == 'date'">{{ item.text  }}日</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

const emit = defineEmits(['update:selected'])
const props = defineProps({
    typevalue: {
        type: String,
        required: true
    },
    list: ref([]),
    selected: String | Number,
});
// 控制旋转
var optionConH = ref(0);
var rot = ref(0);
var ischose = ref(false);
// 切换显示下拉选择区域
function changeoption() {
    if (!ischose.value) {
        rot.value = 90;
        optionConH.value = 300;
    } else {
        optionConH.value = 0;
        rot.value = 0;
    }
    ischose.value = !ischose.value;
}

function handelSelect(item){
    emit('update:selected',item)
    rot.value = 0;
    optionConH.value = 0;
    ischose.value = false;
}

</script>

<style scoped>
.picker {
    width: 100px;
    height: 30px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: space-around;
    color: #fff;
    background-color: #2dafdfc2;
    border-radius: 20px;
    font-weight: 600;
    font-size: 18px;
    line-height: 30px;
}

.select {
    width: 20px;
    height: 20px;
    transition: all 0.5s;
}

.year,
.month,
.date {
    position: relative;
    cursor: pointer;
}

.year-choose-con,
.month-choose-con,
.date-choose-con {
    position: absolute;
    top: 50px;
    left: 0;
    text-align: center;
    width: 110px;
    overflow: hidden;
    transition: all 0.5s;
    z-index: 99;
}

.year-choose,
.month-choose,
.date-choose {
    font-size: 18px;
    font-weight: 600;
    color: #2dafdfc2;
    width: 130px;
    height: 270px;
    overflow: auto;
    overflow-y: scroll;
}

.year-items,
.month-items,
.date-items {
    width: 100px;
    height: 30px;
    color: #fff;
    text-align: center;
    transition: all 0.3s;
    line-height: 30px;
    margin-bottom: 10px;
    background-color: rgba(36, 74, 126, 0.7);
}

.year-items:hover,.month-items:hover,.date-items:hover {
    background-color: #2dafdfc2;
    cursor: pointer;
    border-radius: 10px;
}</style>